
package com.gofun.framework.web.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.SerializationFeature;
import com.gofun.framework.core.utils.BeanFactoryUtils;
import com.gofun.framework.core.utils.StringUtils;

public class ResponseInterceptor implements HandlerInterceptor
{
	final Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
	{

		boolean isPrettyPrint = false;
		String isDebug = request.getParameter("debug");
		if (StringUtils.hasText(isDebug) && isDebug.equalsIgnoreCase("Y"))
		{
			isPrettyPrint = true;
		}
		MappingJackson2HttpMessageConverter conv = BeanFactoryUtils.getBean(MappingJackson2HttpMessageConverter.class);
		conv.getObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, isPrettyPrint);

		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
	{

	}

	@Override
	public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception
	{
		// XXX Auto-generated method stub

	}
}
